package dcutils.rpg.items;

// Import DCUtils Classes
import dcutils.rpg.currency.Currency;

/**
 * Represents a single item.<br/>
 * @author dca
 */
public class Item {
   private int id; // unique identifier
   protected String name; // item name
   protected ItemType type; // item type
   protected Currency value; // can be null (unsellable)

   public Item(String name, ItemType type) {
      this((int)System.currentTimeMillis(), name, type, null);
   } // END constructor
   public Item(int id, String name, ItemType type) {
      this(id, name, type, null);
   } // END constructor
   public Item(String name, ItemType type, Currency value) {
      this((int)System.currentTimeMillis(), name, type, value);
   } // END constructor
   public Item(int id, String name, ItemType type, Currency value) {
      setId(id);
      setName(name);
      setType(type);
      setValue(value);
   } // END constructor

   public int getId() {
      return id;
   } // END getId
   public Item setId(int id) {
      this.id = id;
      return this;
   } // END setId

   public String getName() {
      return name;
   } // END getName
   public Item setName(String name) {
      this.name = name;
      return this;
   } // END setName

   public ItemType getType() {
      return type;
   } // END getType
   public Item setType(ItemType type) {
      this.type = type;
      return this;
   } // END setType

   public Currency getValue() {
      return value;
   } // END getValue
   public Item setValue(Currency value) {
      this.value = value;
      return this;
   } // END setValue
   public boolean hasValue() {
      return null != value;
   } // END hasValue

   public boolean canPurchase(Currency cash) {
      if(null == value || null == cash) return false;
      return cash.canAfford(value);
   } // END canPurchase

   public boolean purchase(Currency cash) {
      if(canPurchase(cash)) {
         cash.subtract(value);
         return true;
      } else {
         return false;
      } // END if/else
   } // END purchase

   @Override
   public String toString() {
      return String.format("%s [%s]", name, type.getName());
   } // END toString

   @Override
   public boolean equals(Object obj) {
      if(null == obj) {
         return false;
      } else if(obj instanceof Item) {
         Item item = (Item)obj;
         return this.getName().equalsIgnoreCase(item.getName());
      } else {
         return false;
      } // END if/else
   } // END equals

   @Override
   public int hashCode() {
      int hashCode = 101;
      hashCode = (11 * hashCode) + (null == getName() ? -1  : getName().hashCode());
      hashCode = (11 * hashCode) + (null == getType() ? -1  : getType().hashCode());
      hashCode = (11 * hashCode) + (null == getValue() ? -1 : getValue().hashCode());
      return hashCode;
   } // END hashCode
} // END class Item
